<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2021/7/26
 * Time: 16:52
 */

namespace app\admin\controller\partner;


use app\common\controller\Backend;
use app\common\model\StoreCate;

class CaseList extends Backend
{
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('PartnerCase');
        $action = $this->request->action();
        if($action == 'add' || $action == 'edit'){
            $this->assign("cateList",model('StoreCate')->where('status',StoreCate::NORMAL)->order('weigh desc, id desc')->column('id,name'));
        }
    }

    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams(['id', 'name'], false);
            $total = $this->model
                ->where($where)
                ->count();
            $list = $this->model
                ->where($where)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();

            $list = collection($list)->toArray();
            foreach($list as &$item){
                $item['cate_name'] = StoreCate::where('id',$item['cate_id'])->value('name');

            }
            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
        return $this->view->fetch();
    }
}